<template>
<h3 style="text-align: center;">
    登录页面
</h3>
<table class="table-bordered" border="1">
<tr>
    <td>
        登录名称
    </td>
    <td>
        <input type="text" v-model="Info.T_Name" class="form-control" placeholder="登录名称">
    </td>
</tr>
<tr>
    <td>
        密码
    </td>
    <td>
        <input type="password" v-model="Info.T_Pwd" class="form-control" placeholder="密码">
    </td>
</tr>
<tr style="text-align: center;">
    <td colspan="2">
        <button class="btn btn-primary" @click="Logins">登录</button>
    </td>
</tr>
</table>
</template>
<script setup lang="ts">
import {useMainStore}from '../../store/index.ts'
const store=useMainStore()
import { useRouter } from 'vue-router';
const router = useRouter();
import { ref } from 'vue';
const Info=ref({
    T_Name:'',
    T_Pwd:'',
    T_Id:0,    
})
import axios from 'axios';
//登录
const Logins=()=>{
    //用户不能为空
    if(Info.value.T_Name==''){
        alert('用户名不能为空')
        return;
    }
    //密码不能为空且包含数字，字母,特殊字符
    if(Info.value.T_Pwd==''){
        alert('密码不能为空')
        return;
    }
    //验证密码格式
    const reg=/(?=.*[a-z])(?=.*\d)(?=.*[#@!~%^&*])[a-z\d#@!~%^&*]{8,16}/i;
    if(!reg.test(Info.value.T_Pwd)){
        alert('密码格式不正确')
        return;
    }
    

    axios.get('https://localhost:44356/api/Userinfo/Logind',
    {
        params:{
            T_Name:Info.value.T_Name,
            T_Pwd:Info.value.T_Pwd
        }
    }).then((res)=>{
        console.log(res.data)
        if(res.data!=null){
            store.T_Id=res.data.T_Id;
            alert('登录成功')
            CxMage(res.data.T_Id)
        }
        else{
            alert('登录失败')
        }
    })
}
//查询用户信息
const CxMage=(T_Id:number)=>{
    axios.get('https://localhost:44356/api/Userinfo/CxyongHmage',{
        params:{
            T_Id:T_Id
        }

    })
    .then((res)=>{
        if(res.data==0)
        {
            //信息表无数据 添加信息
            router.push('/AddUserInfo')
        }
        else{
            //信息表有数据 跳转

            router.push('/UserInfo')
        }

        console.log(res.data)
    })
}


</script>